/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CoursesDialog.java
 *
 * Created on May 9, 2010, 11:04:03 AM
 */
package org.foment.gem.dialogs;

import java.awt.ComponentOrientation;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.ToolTipManager;
import org.foment.joliage3.newt.Message;
import org.foment.joliage3.sql.Database;

/**
 *
 * @author Dusan
 */
public class CoursesDialog extends javax.swing.JFrame {

    /** Creates new form CoursesDialog */
    public CoursesDialog() {
        initComponents();

        toFront();
        setComponents();
    }

    private void setComponents() {
        sqlComboDepartment.setQuery("SELECT departmentID as keyName, departmentTitle as fieldName FROM department ORDER BY departmentID");
        sqlComboDepartment.listenTables("department");

        sqlComboScienceDomain.setQuery("SELECT scienceDomainID as keyName, scienceDomainTitle as fieldName FROM sciencedomain ORDER BY scienceDomainID");
        sqlComboScienceDomain.listenTables("sciencedomain");

        sqlComboCourse.setQuery("SELECT courseID as keyName, courseTitle as fieldName FROM course ORDER BY courseID");
        sqlComboCourse.listenTables("course");

        ToolTipManager.sharedInstance().setInitialDelay(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlDepartment = new javax.swing.JPanel();
        jTextDepartment = new javax.swing.JTextField();
        sqlComboDepartment = new org.foment.joliage3.newt.SQLComboBox();
        btnNewDepartment = new javax.swing.JButton();
        btnModifyDepartment = new javax.swing.JButton();
        btnDeleteDepartment = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        pnlScienceDomain = new javax.swing.JPanel();
        jTextScienceDomain = new javax.swing.JTextField();
        sqlComboScienceDomain = new org.foment.joliage3.newt.SQLComboBox();
        btnNewScienceDomain = new javax.swing.JButton();
        btnModifyScienceDomain = new javax.swing.JButton();
        btnDeleteScienceDomain = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        pnlCourse = new javax.swing.JPanel();
        btnModifyCourse = new javax.swing.JButton();
        btnDeleteCourse = new javax.swing.JButton();
        sqlComboCourse = new org.foment.joliage3.newt.SQLComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextCourse = new javax.swing.JTextField();
        btnNewCourse = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Editor uzih naucnih oblasti");
        setAlwaysOnTop(true);

        pnlDepartment.setBorder(javax.swing.BorderFactory.createTitledBorder("Editor katedri"));

        sqlComboDepartment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sqlComboDepartmentActionPerformed(evt);
            }
        });

        btnNewDepartment.setText("Unesi novu katedru");
        btnNewDepartment.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnNewDepartmentMouseClicked(evt);
            }
        });

        btnModifyDepartment.setText("Izmeni");
        btnModifyDepartment.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnModifyDepartmentMouseClicked(evt);
            }
        });

        btnDeleteDepartment.setText("Obriši");
        btnDeleteDepartment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteDepartmentActionPerformed(evt);
            }
        });

        jLabel3.setText("Naziv katedre");

        jLabel4.setText("Izaberite katedru ili unesite novu");

        javax.swing.GroupLayout pnlDepartmentLayout = new javax.swing.GroupLayout(pnlDepartment);
        pnlDepartment.setLayout(pnlDepartmentLayout);
        pnlDepartmentLayout.setHorizontalGroup(
            pnlDepartmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDepartmentLayout.createSequentialGroup()
                .addGroup(pnlDepartmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDepartmentLayout.createSequentialGroup()
                        .addContainerGap(498, Short.MAX_VALUE)
                        .addComponent(jLabel4))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDepartmentLayout.createSequentialGroup()
                        .addGap(360, 360, 360)
                        .addComponent(sqlComboDepartment, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE))
                    .addGroup(pnlDepartmentLayout.createSequentialGroup()
                        .addGap(284, 284, 284)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pnlDepartmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDepartmentLayout.createSequentialGroup()
                                .addComponent(btnNewDepartment)
                                .addGap(18, 18, 18)
                                .addComponent(btnModifyDepartment)
                                .addGap(18, 18, 18)
                                .addComponent(btnDeleteDepartment))
                            .addComponent(jTextDepartment, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE))))
                .addContainerGap())
        );
        pnlDepartmentLayout.setVerticalGroup(
            pnlDepartmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDepartmentLayout.createSequentialGroup()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sqlComboDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDepartmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDepartmentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDeleteDepartment)
                    .addComponent(btnModifyDepartment)
                    .addComponent(btnNewDepartment))
                .addContainerGap())
        );

        pnlScienceDomain.setBorder(javax.swing.BorderFactory.createTitledBorder("Editor užih naučnih oblasti"));

        sqlComboScienceDomain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sqlComboScienceDomainActionPerformed(evt);
            }
        });

        btnNewScienceDomain.setText("Unesi novu naučnu oblast");
        btnNewScienceDomain.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnNewScienceDomainMouseClicked(evt);
            }
        });

        btnModifyScienceDomain.setText("Izmeni");
        btnModifyScienceDomain.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnModifyScienceDomainMouseClicked(evt);
            }
        });

        btnDeleteScienceDomain.setText("Obriši");
        btnDeleteScienceDomain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteScienceDomainActionPerformed(evt);
            }
        });

        jLabel1.setText("Naziv naučne oblasti");

        jLabel2.setText("Izaberite naučnu oblast ili unesite novu");

        javax.swing.GroupLayout pnlScienceDomainLayout = new javax.swing.GroupLayout(pnlScienceDomain);
        pnlScienceDomain.setLayout(pnlScienceDomainLayout);
        pnlScienceDomainLayout.setHorizontalGroup(
            pnlScienceDomainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlScienceDomainLayout.createSequentialGroup()
                .addGap(217, 217, 217)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlScienceDomainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlScienceDomainLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addComponent(sqlComboScienceDomain, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlScienceDomainLayout.createSequentialGroup()
                        .addComponent(btnNewScienceDomain)
                        .addGap(18, 18, 18)
                        .addComponent(btnModifyScienceDomain)
                        .addGap(18, 18, 18)
                        .addComponent(btnDeleteScienceDomain))
                    .addComponent(jTextScienceDomain, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlScienceDomainLayout.setVerticalGroup(
            pnlScienceDomainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlScienceDomainLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sqlComboScienceDomain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlScienceDomainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(pnlScienceDomainLayout.createSequentialGroup()
                        .addComponent(jTextScienceDomain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlScienceDomainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnDeleteScienceDomain)
                            .addComponent(btnModifyScienceDomain)
                            .addComponent(btnNewScienceDomain))))
                .addContainerGap())
        );

        pnlCourse.setBorder(javax.swing.BorderFactory.createTitledBorder("Predmeti"));

        btnModifyCourse.setText("Izmeni");
        btnModifyCourse.setToolTipText("<html>Prilikom izmene predmeta, vodite računa<br>da su u padajućim menijima izabrani<br>odgovarajuća katedra i uža naučna oblast<br>kojima predmet pripada");
        btnModifyCourse.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnModifyCourseMouseClicked(evt);
            }
        });

        btnDeleteCourse.setText("Obriši");
        btnDeleteCourse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteCourseActionPerformed(evt);
            }
        });

        sqlComboCourse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sqlComboCourseActionPerformed(evt);
            }
        });

        jLabel5.setText("Naziv predmeta");

        jLabel6.setText("Izaberite predmet ili unesite novi");

        btnNewCourse.setText("Unesi novi predmet");
        btnNewCourse.setToolTipText("<html>Prilikom unosa predmeta, vodite računa<br>da su u padajućim menijima izabrani<br>odgovarajuća katedra i uža naučna oblast<br>kojima predmet pripada");
        btnNewCourse.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnNewCourseMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout pnlCourseLayout = new javax.swing.GroupLayout(pnlCourse);
        pnlCourse.setLayout(pnlCourseLayout);
        pnlCourseLayout.setHorizontalGroup(
            pnlCourseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCourseLayout.createSequentialGroup()
                .addGroup(pnlCourseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCourseLayout.createSequentialGroup()
                        .addContainerGap(499, Short.MAX_VALUE)
                        .addComponent(jLabel6))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCourseLayout.createSequentialGroup()
                        .addGap(325, 325, 325)
                        .addComponent(sqlComboCourse, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCourseLayout.createSequentialGroup()
                        .addGap(217, 217, 217)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextCourse, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCourseLayout.createSequentialGroup()
                        .addContainerGap(366, Short.MAX_VALUE)
                        .addComponent(btnNewCourse)
                        .addGap(18, 18, 18)
                        .addComponent(btnModifyCourse)
                        .addGap(18, 18, 18)
                        .addComponent(btnDeleteCourse)))
                .addContainerGap())
        );
        pnlCourseLayout.setVerticalGroup(
            pnlCourseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCourseLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(sqlComboCourse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlCourseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextCourse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCourseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDeleteCourse)
                    .addComponent(btnModifyCourse)
                    .addComponent(btnNewCourse))
                .addGap(18, 18, 18))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlCourse, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlScienceDomain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlDepartment, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlScienceDomain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlCourse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDeleteScienceDomainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteScienceDomainActionPerformed
        // TODO add your handling code here:
        int deleteValue = sqlComboScienceDomain.getValue();
        if (deleteValue == -1) {
            return; // ako nista nije izabrano
        }
        String message = "Da li ste sigurni da želite da obrišete naučnu oblast?\nBrisanjem naučne oblasti" +
                " obrisaće sve i svi predmeti koji joj pripadaju!" ;
        String options[] = {"Da", "Ne"};
        int option = JOptionPane.showOptionDialog(this, message, "Foment Group", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, 0);

        if (option == JOptionPane.YES_OPTION) {

            // prvo brisemo sve predmete koji pripadaju naucnoj oblasti koju brisemo
            String sqlQuery = "DELETE FROM course WHERE courseID IN (SELECT courseID FROM " +
                    "sciencedomaincourses WHERE sciencedomainID = '" + deleteValue + "') ;";
            try {
                // brisemo kurseve koji pripadaju naucnoj oblasti
                Connection  conn = Database.getConnection();
                
                conn.createStatement().executeUpdate(sqlQuery);
                Database.getTable("course").reload();
                // potom brisemo trojke iz tabele zavisnosti
                sqlQuery = "DELETE FROM sciencedomaincourses WHERE sciencedomainID = '" + deleteValue + "' ;";
                conn.createStatement().executeUpdate(sqlQuery);
                // na kraju brisemo samu stavku iz sciencedomain
                sqlQuery = "DELETE FROM sciencedomain WHERE sciencedomainID = '" + deleteValue + "' ;";
                conn.createStatement().executeUpdate(sqlQuery);
                // ucitavamo tabele ponovo
                Database.getTable("sciencedomain").reload();

                conn.close();
                
            } catch (SQLException ex) {
                ex.printStackTrace();
            }

        }
    }//GEN-LAST:event_btnDeleteScienceDomainActionPerformed

    private void btnDeleteDepartmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteDepartmentActionPerformed
        // TODO add your handling code here:
        int deleteValue = sqlComboDepartment.getValue();
        if (deleteValue == -1) {
            return; // ako nista nije izabrano
        }
        String message = "Da li ste sigurni da želite da obrišete katedru?\nBrisanjem katedre obrisaće se i sve naučne oblasti\nKao i svi predmeti koji joj pripadaju!";
        String options[] = {"Da", "Ne"};
        int option = JOptionPane.showOptionDialog(this, message, "Foment Group", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, 0);

        if (option == JOptionPane.YES_OPTION) {
            String sqlQuery = "DELETE FROM course WHERE courseID IN (SELECT courseID FROM " +
                    "sciencedomaincourses WHERE departmentID = '" + deleteValue + "') ;";
            try {
                Connection  conn = Database.getConnection();
                // prvo brisemo sve kurseve koji odgovaraju katedri koju brisemo
                conn.createStatement().executeUpdate(sqlQuery);
                Database.getTable("course").reload();

                // potom brisemo sve naucne oblasti koje odgovaraju katedri
                sqlQuery = "DELETE FROM sciencedomain WHERE sciencedomainID IN (SELECT sciencedomainID FROM " +
                    "sciencedomaincourses WHERE departmentID = '" + deleteValue + "') ;";

                conn.createStatement().executeUpdate(sqlQuery);
                Database.getTable("sciencedomain").reload();

                // brisemo trojke iz tabele zavisnosti
                sqlQuery = "DELETE FROM sciencedomaincourses WHERE departmentID = '" + deleteValue + "' ;";
                conn.createStatement().executeUpdate(sqlQuery);

                // i brisemo iz tabele department na kraju
                sqlQuery = "DELETE FROM department where departmentID = '" + deleteValue + "' ;";
                conn.createStatement().executeUpdate(sqlQuery);
                conn.close();
                Database.getTable("department").reload();
                
            } catch (SQLException ex) {
                ex.printStackTrace();
            }

        }
    }//GEN-LAST:event_btnDeleteDepartmentActionPerformed

    private void btnDeleteCourseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteCourseActionPerformed
        // TODO add your handling code here:
        // TODO add your handling code here:
        int deleteValue = sqlComboCourse.getValue();
        if (deleteValue == -1) {
            return; // ako nista nije izabrano
        }
        String message = "Da li ste sigurni da želite da obrišete predmet?";
        String options[] = {"Da", "Ne"};
        int option = JOptionPane.showOptionDialog(this, message, "Foment Group", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, 0);

        if (option == JOptionPane.YES_OPTION) {
            String sqlQuery = "DELETE FROM course WHERE courseID = " + deleteValue + ";";
            try {
                Connection  conn = Database.getConnection();
                conn.createStatement().executeUpdate(sqlQuery);
                Database.getTable("course").reload();

                sqlQuery = "DELETE FROM sciencedomaincourses WHERE courseID = '" + deleteValue + "' ;";
                conn.createStatement().executeUpdate(sqlQuery);
                // ubaciti da se osvezava tabela employeecourse ili koja god.
                sqlQuery = "DELETE FROM employeecourse WHERE courseID = '" + deleteValue + "' ;";
                conn.createStatement().executeUpdate(sqlQuery);
                conn.close();
                Database.getTable("employeecourse").reload();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }

        }
    }//GEN-LAST:event_btnDeleteCourseActionPerformed

    private void sqlComboScienceDomainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sqlComboScienceDomainActionPerformed
        // TODO add your handling code here:
        jTextScienceDomain.setText(sqlComboScienceDomain.getSelectedValueName());
    }//GEN-LAST:event_sqlComboScienceDomainActionPerformed

    private void sqlComboCourseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sqlComboCourseActionPerformed
        // TODO add your handling code here:
        jTextCourse.setText(sqlComboCourse.getSelectedValueName());
    }//GEN-LAST:event_sqlComboCourseActionPerformed

    private void btnNewCourseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnNewCourseMouseClicked
        if (jTextCourse.getText().equals("")) {
            return;
        }
        String query = "INSERT INTO course (courseTitle) VALUES ('" + jTextCourse.getText() + "' ) ;";
        try {
            int dep = sqlComboDepartment.getValue();
            int sci = sqlComboScienceDomain.getValue();
            if (dep == -1 || sci == -1) {
                throw new SQLException("Morate izabrati katedru i naučnu oblast!");
            }

            Connection  conn = Database.getConnection();
            conn.createStatement().executeUpdate(query);         
            Database.getTable("course").reload();
                       
            // da posldenja uneta stavka bude izabrana
            sqlComboCourse.setSelectedIndex(sqlComboCourse.getItemCount() - 1);

            // unosimo trojku zavisnosti u sciencedomaincourses
            query = "INSERT INTO sciencedomaincourses VALUES ( '" + sqlComboCourse.getValue() + "', '"
                    + sqlComboScienceDomain.getValue() + "', '" + sqlComboDepartment.getValue() + "');";
            conn.createStatement().executeUpdate(query);
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnNewCourseMouseClicked

    private void btnNewScienceDomainMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnNewScienceDomainMouseClicked
        if (jTextScienceDomain.getText().equals("")) {
            return;
        } else {

            String query = "INSERT INTO sciencedomain (scienceDomainTitle) VALUES ('" + jTextScienceDomain.getText() + "' ) ;";
            try
            {
                Connection  conn = Database.getConnection();
                conn.createStatement().executeUpdate(query);                
                conn.close();
                
                Database.getTable("sciencedomain").reload();
                
                sqlComboScienceDomain.setSelectedIndex(sqlComboScienceDomain.getItemCount() - 1);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_btnNewScienceDomainMouseClicked

    private void btnNewDepartmentMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnNewDepartmentMouseClicked
        // TODO add your handling code here:
        if (jTextDepartment.getText().equals("")) {
            return;
        }
        String query = "INSERT INTO department (departmentTitle) VALUES ('" + jTextDepartment.getText() + "' ) ;";
        try
        {
            Connection  conn = Database.getConnection();
            conn.createStatement().executeUpdate(query);
            conn.close();
           
            Database.getTable("department").reload();            
            // postavljamo izbor na poslednji uneti
            sqlComboDepartment.setSelectedIndex(sqlComboDepartment.getItemCount() - 1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnNewDepartmentMouseClicked

    private void btnModifyDepartmentMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnModifyDepartmentMouseClicked
        // TODO add your handling code here:
        if (jTextDepartment.getText().equals("")) {
            return;
        }

        String sqlQuery = "UPDATE Department SET departmentTitle = '" + jTextDepartment.getText() + "' WHERE departmentID = " + sqlComboDepartment.getValue() + ";";
        try
        {
            Connection  conn = Database.getConnection();
            conn.createStatement().executeUpdate(sqlQuery);
            conn.close();
            Database.getTable("department").reload();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnModifyDepartmentMouseClicked

    private void btnModifyScienceDomainMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnModifyScienceDomainMouseClicked
        // TODO add your handling code here:
        if (jTextScienceDomain.getText().equals("")) {
            return;
        }

        String sqlQuery = "UPDATE scienceDomain SET scienceDomainTitle = '" + jTextScienceDomain.getText() + "' WHERE scienceDomainID = " + sqlComboScienceDomain.getValue() + " ;";
        try
        {
            Connection  conn = Database.getConnection();
            conn.createStatement().executeUpdate(sqlQuery);
            conn.close();
            Database.getTable("sciencedomain").reload();


        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnModifyScienceDomainMouseClicked

    private void btnModifyCourseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnModifyCourseMouseClicked
        // TODO add your handling code here:
        if (jTextCourse.getText().equals("")) {
            return;
        }
        int modifyValue = sqlComboCourse.getValue();
        String sqlQuery = "UPDATE course SET courseTitle = '" + jTextCourse.getText() + "' WHERE courseID = " + modifyValue + ";";
        try
        {
            Connection  conn = Database.getConnection();
            conn.createStatement().executeUpdate(sqlQuery);

            Database.getTable("course").reload();
            sqlComboCourse.setSelectedIndex(sqlComboCourse.getItemCount() - 1);

           sqlQuery = "UPDATE sciencedomaincourses SET sciencedomainID = '" + sqlComboScienceDomain.getValue() + "', " +
                   "departmentID = '" + sqlComboDepartment.getValue() + "' WHERE courseID = '" + modifyValue + "' ;";
           conn.createStatement().executeUpdate(sqlQuery);
           conn.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnModifyCourseMouseClicked

    private void sqlComboDepartmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sqlComboDepartmentActionPerformed
        jTextDepartment.setText(sqlComboDepartment.getSelectedValueName());
    }//GEN-LAST:event_sqlComboDepartmentActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CoursesDialog().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDeleteCourse;
    private javax.swing.JButton btnDeleteDepartment;
    private javax.swing.JButton btnDeleteScienceDomain;
    private javax.swing.JButton btnModifyCourse;
    private javax.swing.JButton btnModifyDepartment;
    private javax.swing.JButton btnModifyScienceDomain;
    private javax.swing.JButton btnNewCourse;
    private javax.swing.JButton btnNewDepartment;
    private javax.swing.JButton btnNewScienceDomain;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextCourse;
    private javax.swing.JTextField jTextDepartment;
    private javax.swing.JTextField jTextScienceDomain;
    private javax.swing.JPanel pnlCourse;
    private javax.swing.JPanel pnlDepartment;
    private javax.swing.JPanel pnlScienceDomain;
    private org.foment.joliage3.newt.SQLComboBox sqlComboCourse;
    private org.foment.joliage3.newt.SQLComboBox sqlComboDepartment;
    private org.foment.joliage3.newt.SQLComboBox sqlComboScienceDomain;
    // End of variables declaration//GEN-END:variables
}
